package com.jack.leetcode.array;

/*** 
 * @author zhangjie
 * @date 2020/3/26 21:35
 */
public class 车的可用捕获量 {
    public static void main(String[] args) {
        char[][] board = {
                {'.','.','.','.','.','.','.','.'},
                {'.','p','p','p','p','p','.','.'},
                {'.','p','p','B','p','p','.','.'},
                {'.','p','B','R','B','p','.','.'},
                {'.','p','p','B','p','p','.','.'},
                {'.','p','p','p','p','p','.','.'},
                {'.','.','.','.','.','.','.','.'},
                {'.','.','.','.','.','.','.','.'},
        };
        char[][] board1 = {
                {'.','.','.','.','.','.','.','.'},
                {'.','.','.','p','.','.','.','.'},
                {'.','.','.','R','.','p','.','.'},
                {'.','.','.','.','.','.','.','.'},
                {'.','.','.','.','.','.','.','.'},
                {'.','.','.','p','.','.','.','.'},
                {'.','.','.','.','.','.','.','.'},
                {'.','.','.','.','.','.','.','.'},
        };
        System.out.println(numRookCaptures(board));
        System.out.println(numRookCaptures(board1));
    }

    public static int numRookCaptures(char[][] board) {
        int x=0,y=0;
        int result = 0;
        for(int i =0;i<8;i++){
            for(int j=0;j<8;j++){
                if(board[i][j]=='R'){
                    x=i;
                    y=j;
                }
            }
        }

        for(int i=y-1;i>=0;i--){
            if(board[x][i]=='B'){
                break;
            }
            if(board[x][i]=='p'){
                result++;
                break;
            }
        }
        for(int j=y+1;j<8;j++){
            if(board[x][j]=='B'){
                break;
            }
            if(board[x][j]=='p'){
                result++;
                break;
            }
        }
        for(int i=x+1;i<8;i++){
            if(board[i][y]=='B'){
                break;
            }
            if(board[i][y]=='p'){
                result++;
                break;
            }
        }
        for(int j=x-1;j>=0;j--){
            if(board[j][y]=='B'){
                break;
            }
            if(board[j][y]=='p'){
                result++;
                break;
            }
        }
        return result;
    }
}
